package com.hangzhou.xyj.ui.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.DynamicModel;
import com.hangzhou.xyj.model.respmodel.MyDynamicSpecialModel;
import com.hangzhou.xyj.net.handler.GetMyCollectHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetMyCommentReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.MyCommentAdapter;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.widget.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class MyCommentActivity extends BaseActivity {
    RecyclerView mRecyclerView;
    private MyCommentAdapter mAdapter;
    private int mPageSize = 10;

    @Override
    public void initTop() {
        enableTitle(true, "我的评论");
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_mycomment);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_my_comment);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置水平分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST
                , R.drawable.line_divider_gray));
        mRecyclerView.setAdapter(mAdapter = new MyCommentAdapter(mRecyclerView, null));
        enableRefresh(true);
    }

    @Override
    public void initDatas() {
        mAdapter.setmPageSize(mPageSize);
        getData(1);//获取第一页的数据
    }

    @Override
    public void initListeners() {
        mAdapter.setOnLoadingListener(new BaseLoadingAdapter.OnLoadingListener() {
            @Override
            public void loading() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int currentPage = mAdapter.getCurrentPage(mPageSize);
                        getData(currentPage);
                    }
                }, 500);
            }
        });
    }

    private void getData(final int currentPage) {
        //向服务器请求数据
        GetMyCommentReq getMyCommentReq = new GetMyCommentReq(mMyUserId, mPageSize, currentPage);
        HttpLoader.getInstance().sendRequest(new GetMyCollectHandler(getMyCommentReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<DynamicModel> list = new ArrayList<>();
                for (MyDynamicSpecialModel.MyDynamicTarget data : ((MyDynamicSpecialModel) model).getMyCollectList()) {
                    if (data.getDynamicModel() != null) {
                        data.getDynamicModel().setMyComment(data.getMyComment());//设置我的评论的内容
                        data.getDynamicModel().setType(data.getUserType());
                        data.getDynamicModel().setH5Type(data.getType());
                        data.getDynamicModel().setMyCommentId(data.getId());
                    }
                    list.add(data.getDynamicModel());
                }
                if (currentPage == 1 && list.isEmpty()) {
                    setNoDataTip(true, "暂时没有评论哦，赶紧去评论吧~");
                } else if (currentPage == 1) {
                    mAdapter.load(list);
                } else {
                    mAdapter.update(list);
                }


            }
            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }
        }, 0));
    }

}
